import {
  fetchOperationList,
  getMenuByPermission,
  getBtnByPermission,
  getOperateByPermission,
  fetchPermissionsByRoleId,
  fetchPermission,
  fetchUserList,
} from '@/services/merchants'
import {
  fetchMenus,
} from '@/services/menu'
import {
  fetchBtnList,
} from '@/services/btn'
import {
  rolePermission,
} from '@/services/role'
import {
  fetchManageList,
} from '@/services/setting/manage'
import {
  fetchGroupType,
} from '@/services/group'
import {
  getStoreList,
} from '@/services/supplier/store/storeList'

const RoleModel = {
  namespace: 'role',
  state: {
    inform: {},
    radioGroup: [],
    selected: [],
    userList: [],
    manages: [],
    supplierList: [],
    storeList: [],
  },
  effects: {
    *fetchUsers({ payload }, { call, put }) {
      const res =  yield call(fetchUserList);
      if (res.code == 0) {
        yield put({
          type: 'changeModelState',
          payload: {
            userList: res.data,
          },
        });
      }
    },
    *fetchStores({ payload }, { call, put }) {
      const res = yield call(getStoreList, payload)
      if (res.code == 0) {        
        yield put({
          type: 'changeModelState',
          payload: {
            storeList: res.data,
          },
        });
      } 
    },
    *fetchManageList ({ payload }, { call, put }) {
      const res =  yield call(fetchManageList);
      if (res.code == 0) {
        yield put({
          type: 'changeModelState',
          payload: {
            manages: res.data,
          },
        });
      }
    },
    *fetchSupplierType ({ payload }, { call, put }) {
      const res =  yield call(fetchGroupType, payload);
      if (res.code == 0) {
        yield put({
          type: 'changeModelState',
          payload: {
            supplierList: res.data,
          },
        });
      }
    },
    *fetchOperateList({ payload }, { call, put }) {
      const res =  yield call(getOperateByPermission, payload);

      if (res.code == 0) {
        const response = yield call(fetchOperationList);
        yield put({
          type: 'changeModelState',
          payload: {
            radioGroup: response.data,
            selected: res.data && res.data[0].id,
          },
        });
      }
    },
    *fetchMenusList({ payload }, { call, put }) {
      const res = yield call(getMenuByPermission, payload)
      if (res.code == 0) {        
        const response = yield call(fetchMenus);
        if (response.code == 0) {
          yield put({
            type: 'changeModelState',
            payload: {
              radioGroup: response.data,
              selected: res.data && res.data[0].id,
            },
          });
        }
      }
    },
    *fetchBtnsList({ payload }, { call, put }) {
      const res = yield call(getBtnByPermission, payload)
      if (res.code == 0) {        
        const response = yield call(fetchBtnList);
        yield put({
          type: 'changeModelState',
          payload: {
            radioGroup: response.data,
            selected: res.data && res.data[0].id,
          },
        });
      } 
    },
    *fetchPermissionList({ payload }, { call, put }) {
      const res = yield call(fetchPermissionsByRoleId, payload)
      const permissions = []
      res.data && res.data.map(p => permissions.push(p.id))

      if (res.code == 0) {        
        const response = yield call(fetchPermission);
        yield put({
          type: 'changeModelState',
          payload: {
            radioGroup: response.data,
            selected: permissions,
          },
        });
      } 
    },
  },
  reducers: {
    saveCurrentUser(state, action) {
      return { ...state, currentUser: action.payload || {} };
    },

    changeModelState(state, action) {
      return {
        ...state,
        ...action.payload
      }
    }
  },
};
export default RoleModel;